package com.ys.controller.web.finance;

import com.ys.config.MenuId;
import com.ys.entity.finance.TcwWanliExchangeRate;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.W;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * 万里汇CNY计算汇率表
 */
@Controller
@RequestMapping("/web/finance/tcwWanliExchangeRate")
public class WebTcwWanliExchangeRateController {
	private static final Logger logger = LoggerFactory.getLogger(WebTcwWanliExchangeRateController.class);

	@Autowired
	private CommonApiService<TcwWanliExchangeRate> commonApiService;

    /*
     * 新增
     */
    @GetMapping("/toAdd")
    public String toAdd(Model model, HttpServletRequest request, HttpServletResponse response) {
        try {
            Map<String, Object> extraParam = new HashMap<>();
            //关联数据
            extraParam.put("status", 1);
            model.addAttribute("onProcess", 0);
            return  commonApiService.toAdd(model, new TcwWanliExchangeRate(), MenuId.TCW_WanliExchangeRate, extraParam, null);
        }catch (Exception e) {
            logger.error("新增页面出错：",e.getMessage(),e);
            return W.error(model, e.getMessage());
        }
    }

    /*
     * 编辑
     */
    @GetMapping("/toEdit")
    public String toEdit(Model model, HttpServletRequest request, HttpServletResponse response, Long id) {
        try {
            Map<String, Object> extraParam = getExtraParam(id);
            model.addAttribute("onProcess", 0);
            return commonApiService.toEdit(model,id, new TcwWanliExchangeRate(), MenuId.TCW_WanliExchangeRate, extraParam, null);
        }catch (Exception e) {
            logger.error("编辑页面出错：",e.getMessage(),e);
            return W.error(model, e.getMessage());
        }
    }


    /*
     * 详情
     */
    @GetMapping("/toDetail")
    public String toDetail(Model model, HttpServletRequest request, HttpServletResponse response, Long id) {
        try {
            Map<String, Object> extraParam = getExtraParam(id);
            model.addAttribute("onProcess", 1);
            return commonApiService.toDetail(model,id, new TcwWanliExchangeRate(), MenuId.TCW_WanliExchangeRate, extraParam, null);
        }catch (Exception e) {
            logger.error("详情页面出错：",e.getMessage(),e);
            return W.error(model, e.getMessage());
        }
    }


    public  Map<String,Object> getExtraParam(Long id){
        Map<String,Object> entity = new TcwWanliExchangeRate()

        .addLeftJoin("TCW_DepositPlatform D ON D.Code = A.DepositPlatformCode ")//万里汇账号资金
                .addField("D.PlatformName AS platformName")//收款平台名称
                .addField("D.Code AS depositPlatformCode")//账号编码
                .addField("D.Name AS name")//账号名称
                .addField("D.Mid AS depositPlatformMid")

                .addLeftJoin("TB_CompanyInfo O ON O.Vid = D.CompanyVid ")//公司信息表
                .addField("O.Vname AS vname")//公司名称

                .setId(id)
                .queryFirstMap();

        Map<String, Object> extraParam = new HashMap<>();
        //关联数据
        if(entity.get("platformName")!=null)extraParam.put("platformName", entity.get("platformName"));
        if(entity.get("depositPlatformCode")!=null)extraParam.put("depositPlatformCode", entity.get("depositPlatformCode"));
        if(entity.get("depositPlatformMid")!=null)extraParam.put("depositPlatformMid", entity.get("depositPlatformMid"));
        if(entity.get("name")!=null)extraParam.put("name", entity.get("name"));
        if(entity.get("vname")!=null)extraParam.put("vname", entity.get("vname"));
        return extraParam;
    }


}






